package com.hanbang.gycatering.classify;

import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.hanbang.gangyu.R;
import com.hanbang.gycatering.aboutme.AboutMe;
import com.hanbang.gycatering.application.MyApplication;
import com.hanbang.gycatering.bean.OneLevelData;
import com.hanbang.gycatering.bean.TwoLevelData;
import com.hanbang.gycatering.homepage.MainBaseActivity;
import com.hanbang.gycatering.homepage.HomePageActivity;
import com.hanbang.gycatering.myviews.NoScrollGridView;
import com.hanbang.gycatering.property.PropertyService;
import com.hanbang.gycatering.shoppingcart.ShoppingCart;
import com.hanbang.gycatering.utils.HttpInterfaces;
import com.hanbang.gycatering.utils.MyRequestCallBack;
import com.hanbang.gycatering.utils.ShareMethod;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ClassifyActivity extends MainBaseActivity {
	private ViewGroup ll_one, ll_two, ll_three, ll_four, ll_five;
	private PullToRefreshListView listView = null;
	private MyAdapter adapter;
	private ArrayList<OneLevelData> oneLevels = new ArrayList<OneLevelData>();
	private ArrayList<ArrayList<TwoLevelData>> twoLevels = new ArrayList<ArrayList<TwoLevelData>>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.classify);
		initView();
		setTheClick();
		getCategraylist();

	}

	/*
	 * 获取分类信息 （可以做数据库缓存）
	 */
	private void getCategraylist() {
		((MyApplication) getApplicationContext()).httpUtils.send(
				HttpMethod.GET, HttpInterfaces.categraylist,
				new MyRequestCallBack<String>(ClassifyActivity.this,
						getString(R.string.loadding)) {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							if (json.getInt("state") == 1) {
								JSONArray info = json.getJSONArray("info");
								for (int i = 0; i < info.length(); i++) {
									// 获取父类
									OneLevelData data = new OneLevelData();
									JSONObject categrayone = info
											.getJSONObject(i)
											.getJSONArray("categrayone")
											.getJSONObject(0);
									data.setId(categrayone.getInt("id"));
									data.setImg(categrayone.getString("img"));
									data.setName(categrayone.getString("name"));
									oneLevels.add(data);
									// 获取子类
									JSONArray categraytwo = info.getJSONObject(
											i).getJSONArray("categraytwo");
									ArrayList<TwoLevelData> twoS = new ArrayList<TwoLevelData>();
									for (int j = 0; j < categraytwo.length(); j++) {
										TwoLevelData tData = new TwoLevelData();
										JSONObject two = categraytwo
												.getJSONObject(j);
										tData.setId(two.getInt("id"));
										tData.setName(two.getString("name"));
										twoS.add(tData);
									}
									int size = (twoS.size() % 3 == 0 ? 0
											: 3 - twoS.size() % 3);
									for (int j = 0; j < size; j++) {
										TwoLevelData tData = new TwoLevelData();
										twoS.add(tData);
									}
									twoLevels.add(twoS);
								}
							}
							adapter.notifyDataSetChanged();
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});

	}

	private void initView() {
		// TODO Auto-generated method stub

		((TextView) findViewById(R.id.top_bar_title)).setText("分类");
		((ImageView) findViewById(R.id.top_bar_left_image))
				.setVisibility(View.GONE);
		ll_one = (ViewGroup) findViewById(R.id.bottom_bar_ll1);
		ll_two = (ViewGroup) findViewById(R.id.bottom_bar_ll2);
		ll_three = (ViewGroup) findViewById(R.id.bottom_bar_ll3);
		ll_four = (ViewGroup) findViewById(R.id.bottom_bar_ll4);
		ll_five = (ViewGroup) findViewById(R.id.bottom_bar_ll5);
		listView = (PullToRefreshListView) findViewById(R.id.listview);
		listView.setMode(Mode.DISABLED);
		listView.setOnItemClickListener(onItemClickListener);
		adapter = new MyAdapter();
		listView.setAdapter(adapter);
	}

	// 上一次展开的VIew
	View gridShang = null;
	int positionShang = -1;
	OnItemClickListener onItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, final View view,
				final int position, long id) {
			MyAdapter.ViewHolder holder = (MyAdapter.ViewHolder) view.getTag();
			if (positionShang != position - 1) {
				if (gridShang != null && positionShang != -1) {
					((MyAdapter.ViewHolder) gridShang.getTag()).gridview
							.setVisibility(View.GONE);
					((MyAdapter.ViewHolder) gridShang.getTag()).view
							.setVisibility(View.GONE);
					gridShang.setBackgroundResource(R.color.translucent);
					// 改变isGridViewVisibility
					oneLevels.get(positionShang).setGridViewVisibility(false);

				}
				expandItem(view, holder, position);
			} else {
				if (gridShang != null && positionShang != -1) {
					((MyAdapter.ViewHolder) gridShang.getTag()).gridview
							.setVisibility(View.GONE);
					((MyAdapter.ViewHolder) gridShang.getTag()).view
							.setVisibility(View.GONE);
					gridShang.setBackgroundResource(R.color.translucent);
					// 改变isGridViewVisibility
					oneLevels.get(positionShang).setGridViewVisibility(false);
					gridShang = null;
					positionShang = -1;
				} else {

					expandItem(view, holder, position);
				}
			}
		}
	};

	private void expandItem(View view, MyAdapter.ViewHolder holder,
			final int position) {
		holder.view.setVisibility(View.VISIBLE);
		holder.gridview.setVisibility(View.VISIBLE);
		view.setBackgroundResource(R.color.gray_f8);
		// 改变isGridViewVisibility
		oneLevels.get(position - 1).setGridViewVisibility(true);
		gridShang = view;
		positionShang = position - 1;
	}

	private void setTheClick() {
		ll_one.setOnClickListener(myOncClickListener);
		ll_two.setOnClickListener(myOncClickListener);
		ll_three.setOnClickListener(myOncClickListener);
		ll_four.setOnClickListener(myOncClickListener);
		ll_five.setOnClickListener(myOncClickListener);
	}

	private OnClickListener myOncClickListener = new OnClickListener() {
		@Override
		public void onClick(View view) {
			Intent intent = new Intent();
			switch (view.getId()) {
			case R.id.bottom_bar_ll1:
				intent.setClass(ClassifyActivity.this, HomePageActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				break;
			case R.id.bottom_bar_ll3:
				intent.setClass(ClassifyActivity.this, PropertyService.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				break;
			case R.id.bottom_bar_ll4:
				intent.setClass(ClassifyActivity.this, ShoppingCart.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				break;
			case R.id.bottom_bar_ll5:
				intent.setClass(ClassifyActivity.this, AboutMe.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				break;
			default:
				break;
			}
		}
	};

	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return oneLevels.size();
		}

		@Override
		public Object getItem(int position) {
			return oneLevels.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@SuppressLint("InflateParams")
		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			ViewHolder holder = null;
			OneLevelData data = oneLevels.get(position);
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = getLayoutInflater().inflate(
						R.layout.classify_list_item, null);
				holder.imageView = (ImageView) convertView
						.findViewById(R.id.image);
				holder.title = (TextView) convertView.findViewById(R.id.title);
				holder.gridview = (NoScrollGridView) convertView
						.findViewById(R.id.gridview);
				holder.view = convertView.findViewById(R.id.view);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			if (data.isGridViewVisibility()) {
				holder.gridview.setVisibility(View.VISIBLE);
				holder.view.setVisibility(View.VISIBLE);
				convertView.setBackgroundResource(R.color.gray_f8);
			} else {
				holder.gridview.setVisibility(View.GONE);
				holder.view.setVisibility(View.GONE);
				convertView.setBackgroundResource(R.color.translucent);
			}
			holder.title.setText(data.getName());
			holder.gridview.setAdapter(new GridViewAdapter(position));
			bitmapUtils.display(holder.imageView,
					HttpInterfaces.ORG + data.getImg());
			holder.gridview.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int pos, long id) {
					Intent intent = new Intent(ClassifyActivity.this,
							ClassifyList.class);
					if (twoLevels.get(position).get(pos).getName() != null) {
						intent.putExtra("id", twoLevels.get(position).get(pos)
								.getId());
						intent.putExtra("httpBase", HttpInterfaces.productlist);
						intent.putExtra("title",
								twoLevels.get(position).get(pos).getName());
						startActivity(intent);
					}

				}
			});
			return convertView;
		}

		private class ViewHolder {
			ImageView imageView = null;
			TextView title = null;
			NoScrollGridView gridview = null;
			View view = null;
		}
	}

	class GridViewAdapter extends BaseAdapter {
		ArrayList<TwoLevelData> twoLevelDatas = null;
		int listPosition = 0;

		public GridViewAdapter(int position) {
			twoLevelDatas = twoLevels.get(position);
			this.listPosition = position;
		}

		@Override
		public int getCount() {
			if (twoLevelDatas == null) {
				return 0;
			}
			return twoLevelDatas.size();
		}

		@Override
		public Object getItem(int position) {
			if (twoLevelDatas == null) {
				return null;
			}
			return twoLevelDatas.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@SuppressLint("InflateParams")
		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = getLayoutInflater().inflate(
						R.layout.classify_gridview_item, null);
				holder.title = (TextView) convertView.findViewById(R.id.title);
				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			if (twoLevelDatas.get(position).getName() != null) {
				holder.title.setText(twoLevelDatas.get(position).getName());
				holder.title.setEnabled(true);
			} else {
				holder.title.setEnabled(false);
			}

			return convertView;

		}

		private class ViewHolder {
			TextView title;
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		ShareMethod.setButton(ll_two, false);

	};

	@Override
	protected void onPause() {
		super.onPause();
		ShareMethod.setButton(ll_two, true);
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

}
